﻿using PLCS.Application.Contracts.StationApp.Dtos;
using PLCS.Application.Contracts.TrayApp.Dtos;
using PLCS.Client.IService.EquipmentManager;
using PLCS.Domain.Shared.Stations;
using PLCS.Domain.Stations;

using Volo.Abp.Application.Dtos;

namespace PLCS.Client.Service.EquipmentManager;

public class TrayService : BaseService<TrayDto, TrayEditDto>, ITrayService
{
    public async Task CreateListAsync(IEnumerable<TrayEditDto> trays)
    {
        RequestParam requestParam = new RequestParam(method: Method.Post, route: $"tray/list", parameter: trays);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task DisassociateAsync(TrayDto tray)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"tray/disassociate", tray);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<PagedResultDto<TrayDto>> GetListBySearchAsync(int skipCount, int maxResultCount, string? trayType, string? trayNo)
    {
        var route = $"tray/bySearch?SkipCount={skipCount}&MaxResultCount={maxResultCount}";
        if (!string.IsNullOrEmpty(trayType))
            route += $"&trayType={trayType}";
        if (!string.IsNullOrEmpty(trayNo))
            route += $"&trayNo={trayNo}";

        RequestParam requestParam = new(method: Method.Get, route: route);
        var apiRes = await restClient.ExecuteAsync<PagedResultDto<TrayDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return new PagedResultDto<TrayDto>();
    }

    public async Task TrayBindAsync(TrayDto tray)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"tray/trayBind", tray);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }
}